package com.baidu.camera.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


import com.baidu.camera.R;
import com.baidu.camera.utils.MetricsUtils;

import org.androidannotations.annotations.EView;

/**
 * Created by Kvin on 2016/2/16.
 */
@EView
public class DrawUi extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder holder;
    private Paint paint;
    private DisplayMetrics m;

    public DrawUi(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        holder = this.getHolder();
        holder.addCallback(this);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

        m = MetricsUtils.winMetrics(context);
        gatherTransparentRegion(new Region(10, 40, m.widthPixels - 10, m.heightPixels / 2 + 40));
    }

    public DrawUi(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    @Override
    public void surfaceCreated(final SurfaceHolder holder) {
        //draw UI
        new Thread() {
            @Override
            public void run() {
                super.run();
                drawPicture(holder.lockCanvas());
            }
        }.start();

    }

    private void drawPicture(Canvas canvas) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        try {
            canvas.drawBitmap(bitmap, 10, 10, paint);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            holder.unlockCanvasAndPost(canvas);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public SurfaceHolder getHolder() {
        return holder;
    }
}
